package cn.cvs.controller;

import cn.cvs.pojo.TSysUser;
import cn.cvs.service.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class Ch10Controller {

    List<String> userList = new ArrayList<>();

    public Ch10Controller(){
        userList.add("王春琳");
        userList.add("谭美兰");
        userList.add("王颖");
        userList.add("黎惠芬");
        userList.add("chengmenglu");
        userList.add("liushuti");
    }

    @RequestMapping("/toUserList")
    public String getUserList(Model model, HttpServletRequest request){
        model.addAttribute("userList",userList);
        request.setAttribute("ulist",userList);
        return "user/ch10_userlist.jsp";
    }

    //@RequestMapping("/showUserList")  get/post 请求都可以进来
    @PostMapping("showUserList")
    public String showUserList(Model model,@RequestParam(required = false) String realName){  // 王

        System.out.println(">>>>>>>>>...Ch10Controller.showUserList.realName : "+realName);

        List<String> queryList =new ArrayList<>();

        if (null==realName || realName.length()==0|| "".equals(realName)){
            queryList.addAll(userList);
        }else{
            userList.forEach(user->{
                if (user.indexOf(realName) !=-1){  //在用户列表 userList 找到 了,传过来的王
                    //添加到一个行的集合中去存起来
                    queryList.add(user);//添加用户
                }
            });

        }
        model.addAttribute("userList",queryList);

        return "user/ch10_userlist.jsp";
    }

    @Resource
    SysUserService userService ;// new ??
    //获取数据库中的用户列表
    @RequestMapping("/dbUserList")
    public String dbUserList(Model model){
        List<TSysUser> userList=userService.getUserList(new TSysUser());
        model.addAttribute("userList",userList);
        return "user/ch10_userlist.jsp";
    }

}
